<template>
	<view>
		<uvsticky offset-top="0">
			<view class="cinematop">
				<image :src="datainfo.pic" class="cinimg"></image>
				<view class="cinema-info">
					<view class="mo-info-top">{{datainfo.filmName}}</view>
					<view class="uiycss"> 评分：{{datainfo.score}} </view>
					<view class="uiycss"> {{datainfo.filmType}} | {{datainfo.duration}}分钟 </view>
					<view class="uiycss"> {{datainfo.publishDate}}上映 </view>
				</view>
			</view>
		</uvsticky>
		<view class="centcss">
			<view class="yycss" v-for="item in yyList" :key="item.cinemaId" @click="go(item)">
				<view class="dyname">
					<text>{{item.cinemaName}}</text>
					<text>{{item.formattedDistance}}</text>
				</view>
				<view class="txt">{{item.address}}</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref , onMounted } from 'vue';
	import { onShow , onLoad , onReachBottom } from '@dcloudio/uni-app';
	import { apiGetmodet , apiDress , apiMoviefilmid } from '@/api/upapi.js';
	import { calculateDistance } from '@/utils/distance.js';
	import uvsticky from '@/uni_modules/uv-sticky/components/uv-sticky/uv-sticky';

	const datainfo = ref({});
	const yyList = ref([]);
	const lat = ref('');
	const idx = ref('');
	const lng = ref('');
	const pages =ref(1);
	const isNext = ref(false);

	const getDetail =async()=>{
		try {
			let res = await apiGetmodet({ filmId: idx.value });
			if(res.data.result==0){
				datainfo.value=res.data.object
			}else{
				uni.showToast({ title: res.data.msg, icon:'none' });
			}
		} catch (error) {
			console.error('获取数据失败', error);
		}
	};
	const getLocadin = () => {
		uni.getLocation({
			type: 'wgs84',
			success(res) {
				lat.value = res.latitude;
				lng.value = res.longitude;
				getDress();
			},
			fail(error) {
				console.log('error===',error);
			}
		});
	};
	const go=(e)=>{ uni.navigateTo({ url:'/pagesA/movie/moveinfo?data=' + encodeURIComponent(JSON.stringify(e))+'&filmId='+datainfo.value.filmId }) };
	const getDress =async()=>{
		const ctiyid = uni.getStorageSync('cityIds');
		try {
			let res = await apiDress({ 
				cityId: ctiyid,
				// filmId:idx.value,
				page:pages.value,
				pageSize:10
			});
			if(res.data.result==0){
				const data=res.data.object.map(cinema => ({
				    ...cinema,
				    distance: calculateDistance(
				      lat.value,
				      lng.value,
				      parseFloat(cinema.latitude),
				      parseFloat(cinema.longitude)
				    )
				})).sort((a, b) => a.distance - b.distance);
				const formattedData = data.map(cinema => ({
				    ...cinema,
				    formattedDistance: formatDistance(cinema.distance)
				}));
				// yyList.value=formattedData
				if (pages.value == 1) {
				    yyList.value = formattedData;
				} else {
				    yyList.value = yyList.value.concat(formattedData);
				}
				if (formattedData.length >= 10) {
				    pages.value += 1;
				} else {
				    isNext.value = true;
				}
			}else{
				uni.showToast({ title: res.data.msg, icon:'none' });
			}
		} catch (error) {
			console.error('获取数据失败', error);
		}
	};
	const formatDistance = (meters) => {
		const kilometers = meters / 1000;
		return `${kilometers.toFixed(1)}km`;
	};

	onLoad((option) => {
		uni.setNavigationBarTitle({ title: option.title });
		idx.value=option.id;
		getDetail();
		getLocadin();
	});
	onReachBottom(() => {
		if (!isNext.value) { getDress(); }
	});
</script>

<style lang="scss" scoped>
	.cinematop {
		padding: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #636775;

		.cinimg {
			width: 180rpx;
			height: 250rpx;
			border-radius: 20rpx;
		}

		.cinema-info {
			flex: 1;
			margin-left: 20rpx;
			height: 250rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.uiycss {
				color: #ffffff;
				font-size: 24rpx;
			}

			.mo-info-top {
				color: #ffffff;
				font-size: 28rpx;
				font-weight: 600;
				width: 360rpx;
				display: -webkit-box;
				/* 使用弹性盒子布局 */
				-webkit-box-orient: vertical;
				/* 垂直排列 */
				-webkit-line-clamp: 2;
				/* 限制为两行 */
				overflow: hidden;
				/* 超出部分隐藏 */
				text-overflow: ellipsis;
			}
		}
	}

	.centcss {
		padding: 22rpx;
	}

	.yycss {
		background-color: #fff;
		padding: 20rpx;
		border-radius: 20rpx;
		margin-bottom: 20rpx;

		.dyname {
			display: flex;
			align-items: center;
			justify-content: space-between;

			>text:first-child {
				font-size: 28rpx;
				font-weight: 600;
				width: 80%;
				white-space: nowrap;
				/* 禁止换行 */
				overflow: hidden;
				/* 隐藏溢出内容 */
				text-overflow: ellipsis;
				/* 显示省略号 */
			}

			>text:last-child {
				font-size: 26rpx;
			}
		}

		.txt {
			font-size: 22rpx;
			text-align: justify;
			text-justify: inter-ideograph;
			line-height: 1.3;
			margin-top: 10rpx;
			color: #7d7d7d;
		}
	}
</style>